package info.androidhive.slidingmenu;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import info.androidhive.slidingmenu.model.ServiceHandler;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class FindPeopleFragment extends Fragment {
	
	public FindPeopleFragment(){}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
 
        View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
         
        return rootView;
    }
	
	private ProgressDialog pDialog;
	
	private static String url = "http://127.0.0.1/zz.php?mssv=3110410130&option=xemthongtincanhan";
	
	private static final String TAG_THONGTINCANHAN = "thongtincanhan";
	private static final String TAG_MASINHVIEN = "masinhvien";
	private static final String TAG_TENSINHVIEN = "tensinhvien";
	private static final String TAG_LOP = "lop";
	private static final String TAG_NGANH = "nganh";
	private static final String TAG_KHOA = "khoa";
	private static final String TAG_HEDAOTAO = "hedaotao";
	private static final String TAG_KHOAHOC = "khoahoc";
	
	JSONArray thongtincanhan = null;
	
	ArrayList<HashMap<String, String>> thongtincanhanList;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		setContentView(R.layout.fragment_find_people);
		thongtincanhanList = new ArrayList<HashMap<String, String>>();
		new GetThongTinCaNhan().execute();
	}
	private class GetThongTinCaNhan extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			//pDialog = new ProgressDialog(MainActivity.this);
			pDialog.setMessage("Please wait...");
			pDialog.setCancelable(false);
			pDialog.show();
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			// Creating service handler class instance
			ServiceHandler sh = new ServiceHandler();

			// Making a request to url and getting response
			String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

			Log.d("Response: ", "> " + jsonStr);

			if (jsonStr != null) {
				try {
					JSONObject jsonObj = new JSONObject(jsonStr);
					
					// Getting JSON Array node
					thongtincanhan = jsonObj.getJSONArray(TAG_THONGTINCANHAN);

					// looping through All Contacts
					for (int i = 0; i < thongtincanhan.length(); i++) {
						JSONObject c = thongtincanhan.getJSONObject(i);
						
						String masinhvien = c.getString(TAG_MASINHVIEN);
						String tensinhvien=c.getString(TAG_TENSINHVIEN);
						String lop=c.getString(TAG_LOP);
						String nganh=c.getString(TAG_NGANH);
						String khoa=c.getString(TAG_KHOA);
						String hedaotao=c.getString(TAG_HEDAOTAO);
						String khoahoc=c.getString(TAG_KHOAHOC);
						
						
						HashMap<String, String> thongtincanhan = new HashMap<String, String>();

						// adding each child node to HashMap key => value
						thongtincanhan.put(TAG_MASINHVIEN, masinhvien);
						thongtincanhan.put(TAG_TENSINHVIEN, tensinhvien);
						thongtincanhan.put(TAG_LOP, lop);
						thongtincanhan.put(TAG_NGANH, nganh);
						thongtincanhan.put(TAG_KHOA, khoa);
						thongtincanhan.put(TAG_HEDAOTAO, hedaotao);
						thongtincanhan.put(TAG_KHOAHOC, khoahoc);

						// adding contact to contact list
						thongtincanhanList.add(thongtincanhan);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			} else {
				Log.e("ServiceHandler", "Couldn't get any data from the url");
			}

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			// Dismiss the progress dialog
			if (pDialog.isShowing())
				pDialog.dismiss();
			/**
			 * Updating parsed JSON data into ListView
			 * */
		}

	}
	
}
